package org.example.ychdp.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Value;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
public class WeChatPayController {

//    @Value("${wechat.pay.appid}")
//    private String appId;
//
//    @Value("${wechat.pay.merchantId}")
//    private String merchantId;
//
//    @Value("${wechat.pay.apiKey}")
//    private String apiKey;
//
//    @Value("${wechat.pay.callbackUrl}")
//    private String callbackUrl;
//
//    private static final String UNIFIED_ORDER_URL = "https://api.mch.weixin.qq.com/v3/pay/transactions/native";
//
//    @GetMapping("/createOrder")
//    public Map<String, String> createOrder(@RequestParam String orderId, @RequestParam String amount) throws Exception {
//        // 构造支付订单参数
//        Map<String, Object> orderParams = new HashMap<>();
//        orderParams.put("appid", appId);
//        orderParams.put("mchid", merchantId);
//        orderParams.put("description", "商品描述");
//        orderParams.put("out_trade_no", orderId);
//        orderParams.put("notify_url", callbackUrl);
//        orderParams.put("amount", Map.of("total", Integer.parseInt(amount) * 100, "currency", "CNY"));
//
//        // 调用微信支付接口
//        HttpURLConnection connection = (HttpURLConnection) new URL(UNIFIED_ORDER_URL).openConnection();
//        connection.setDoOutput(true);
//        connection.setRequestMethod("POST");
//        connection.setRequestProperty("Content-Type", "application/json");
//
//        ObjectMapper mapper = new ObjectMapper();
//        String jsonParams = mapper.writeValueAsString(orderParams);
//
//        try (OutputStream os = connection.getOutputStream()) {
//            os.write(jsonParams.getBytes());
//        }
//
//        if (connection.getResponseCode() == 200) {
//            // 解析响应，返回二维码链接
//            Map<String, Object> response = mapper.readValue(connection.getInputStream(), Map.class);
//            String qrCodeUrl = (String) response.get("code_url");
//            return Map.of("qrCodeUrl", qrCodeUrl);
//        } else {
//            throw new RuntimeException("Failed to create order, response code: " + connection.getResponseCode());
//        }
//    }
}
